<?php

namespace App\Models;

use App\Enums\AppMiniPlateEnum;
use App\Exceptions\ApiException;
use App\Exceptions\BaseException;
use App\Models\v1\SpecificationGroup;
use App\Services\CommonService;
use App\Traits\ModelEventTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class AppMiniPlateLog extends BaseModel
{
    use SoftDeletes;
    use ModelEventTrait;

    protected $table = 'sp_app_mini_plate_log'; //表名
    protected $primaryKey = 'id'; //主键
    protected $guarded = [];

    protected $appends = ['mini_ids_name'];

    /**
     * 获取格式化后的配置
     * @return mixed
     */
    public function getMiniIdsAttribute($value)
    {

        return json_decode($value,true);
    }

    public function getMiniIdsNameAttribute()
    {
        return $this->mini_ids?AppTypeModel::query()->whereIn('id',$this->mini_ids)->pluck('app_name')->toArray():[];
    }

    /**
     * 获取格式化后的配置
     * @return mixed
     */
    public function getContentAttribute($value)
    {
        return json_decode($value,true);
    }


    /**
     * 根据配置的分组获取配置
     */
    public function getPlateLogList($key)
    {
        $res=self::query()
            ->orderBy('id','desc')
            ->where('key', $key)
            ->select('key','description','biz_type','range','mini_ids','id','created_at','updated_at')
            ->get();
        $data=[];
        foreach ($res as $k=>$v){

            $i=0;
            foreach ($data as $k1=>$v1){
                if($v['mini_ids']==$v1['mini_ids']&&$v['range']==$v1['range']&&$v['biz_type']==$v1['biz_type']){
                    $i=1;
                    break;
                }
            }
            if($i==1){
                continue;
            }
            $data[]=$v;
        }

        return $data;
    }

    public  function getPlateLogDetail($id)
    {
        $log = AppMiniPlateLog::where('id',$id)->first();
        if(!$log){
            throw new ApiException('配置不存在');
        }

        return $log;

    }
    public  function getPlateLogCategoryDetail($params)
    {
        $log = AppMiniPlateLog::where($params)->orderBy('id','desc')->first();
        if(!$log){
            throw new ApiException('配置不存在');
        }

        return $log;

    }

    public static function deletedPlateLog($id)
    {
        $log = AppMiniPlateLog::find($id);
        if(!$log){
            throw new ApiException('配置不存在');
        }
        return $log->delete();
    }

    /**
     * 通过[key=>value]格式更新配置
     * @param array  $setting_data
     * @param string $group
     * @return bool
     */
    public static function savePlateLog($data){
        Db::beginTransaction();
        try{
            $res= self::create($data);
        }catch (\Exception $e){
            Db::rollBack();
            throw new ApiException('写入失败');
        }
        Db::commit();

        return true;
    }


}
